package cn.lb.permission.controller;


import cn.lb.annotation.LBPermission;
import cn.lb.permission.domain.Permission;
import cn.lb.permission.query.PermissionQuery;
import cn.lb.permission.service.IPermissionService;
import cn.lb.basic.util.PageList;
import cn.lb.basic.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@LBPermission(name = "权限管理")
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 分页查询+高级查询
     * 入参: PermissionQuery currentPage pageSize keyword
     * 返参: R  -> dataObj: PageList
     */
    @LBPermission(name = "分页查询")
    @PostMapping
    public R listPage(@RequestBody PermissionQuery query){
        try {
            // 调用Service做分页查询
            PageList<Permission> pageList = permissionService.listPage(query);
            return R.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }
    @LBPermission(name = "查询权限树")
    @GetMapping("/tree")
    public R tree(){
        try {
            // 调用service权限树查询
            List<Permission> permissions = permissionService.tree();
            return R.me().setData(permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询权限树失败!"+e.getMessage());
        }
    }

}
